package se.teamscape.client.rpc;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * A callback class for our dispatch calls. This provides a convenient way to
 * handle error cases and we can save some code as well. Inspired by
 * HupaCallback in the Apache HUPA project
 * 
 * @author Andreas Borglin
 * 
 */
public abstract class DispatchCallback<T> implements AsyncCallback<T> {

    public DispatchCallback() {

    }

    @Override
    public void onFailure(Throwable caught) {
        callbackError(caught);
    }

    @Override
    public void onSuccess(T result) {
        callback(result);
    }

    /**
     * Must be overriden by clients to handle callbacks
     * 
     * @param result
     */
    public abstract void callback(T result);

    /**
     * Should be overriden by clients who want to handle error cases themselves.
     */
    public void callbackError(Throwable t) {
        t.printStackTrace();
        Window.alert("RPC failed: " + t.toString());
    }
}
